﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraSplashScreen;
using System.Drawing;
using DevExpress.XtraBars.Alerter;

namespace Ozner.UI
{
    public class BoxHelper
    {
        private static int AlertCount = 0;
        private static AlertButtonOption[] btnOpts;

        #region Alert
        /// <summary>
        /// Alert一个对话框
        /// </summary>
        /// <param name="owner">父容器</param>
        /// <param name="caption">标题</param>
        public static void Alert(Form owner, string caption)
        {
            Alert(owner, caption, "", 5000);
        }
        /// <summary>
        /// Alert一个对话框
        /// </summary>
        /// <param name="owner">父容器</param>
        /// <param name="caption">标题</param>
        /// <param name="descrption">描述</param>
        public static void Alert(Form owner, string caption, string description)
        {
            Alert(owner, caption, description, 5000);
        }
        /// <summary>
        /// Alert一个对话框
        /// </summary>
        /// <param name="owner">父容器</param>
        /// <param name="caption">标题</param>
        /// <param name="descrption">描述</param>
        /// <param name="autoFormDelay">自动关闭延时(毫秒),默认5000</param>
        public static void Alert(Form owner,
            string caption,
            string description,
            int autoFormDelay)
        {
            Alert(owner, caption, description, autoFormDelay, true);
        }
        /// <summary>
        /// Alert一个对话框
        /// </summary>
        /// <param name="owner">父容器</param>
        /// <param name="caption">标题</param>
        /// <param name="descrption">描述</param>
        /// <param name="autoFormDelay">自动关闭延时(毫秒),默认5000</param>
        /// <param name="autoCloseFormOnClick">点击后关闭窗体</param>
        public static void Alert(Form owner,
            string caption,
            string description,
            int autoFormDelay,
            bool autoCloseFormOnClick)
        {
            Alert(owner, caption, description, autoFormDelay, autoCloseFormOnClick, true);
        }

        /// <summary>
        /// Alert一个对话框
        /// </summary>
        /// <param name="owner">父容器</param>
        /// <param name="caption">标题</param>
        /// <param name="descrption">描述</param>
        /// <param name="autoFormDelay">自动关闭延时(毫秒),默认5000</param>
        /// <param name="autoCloseFormOnClick">点击后关闭窗体</param>
        /// <param name="showCloseButton">显示关闭按钮</param>
        public static void Alert(Form owner,
            string caption,
            string description,
            int autoFormDelay,
            bool autoCloseFormOnClick,
            bool showCloseButton)
        {
            Alert(owner, caption, description, autoFormDelay, autoCloseFormOnClick, showCloseButton,true);
        }

        /// <summary>
        /// Alert一个对话框
        /// </summary>
        /// <param name="owner">父容器</param>
        /// <param name="caption">标题</param>
        /// <param name="descrption">描述</param>
        /// <param name="autoFormDelay">自动关闭延时(毫秒),默认5000</param>
        /// <param name="autoCloseFormOnClick">点击后关闭窗体</param>
        /// <param name="showCloseButton">显示关闭按钮</param>
        /// <param name="showPinButton">显示固定按钮</param>
        /// <param name="btnOpts">按钮组</param>
        public static void Alert(
            Form owner,
            string caption,
            string descrption,
            int autoFormDelay,
            bool autoCloseFormOnClick,
            bool showCloseButton,
            bool showPinButton,
            params AlertButtonOption[] btnOpts)
        {
            AlertControl alert = new AlertControl();
            alert.ShowCloseButton = showCloseButton;
            alert.ShowPinButton = showPinButton;
            alert.AutoFormDelay = autoFormDelay;
            alert.FormDisplaySpeed = AlertFormDisplaySpeed.Slow;
            alert.FormShowingEffect = AlertFormShowingEffect.SlideVertical;
            if(btnOpts != null && btnOpts.Length > 0)
            {
                foreach (AlertButtonOption abo in btnOpts)
                {
                    AlertButton ab = null;
                    //判断图像
                    if (abo.Image != null)
                    {
                         ab = new AlertButton(abo.Image);
                    }else
                    {
                        ab = new AlertButton();
                    }
                    ab.Hint = abo.Hint;
                    //判断name是否空
                    if (string.IsNullOrEmpty(abo.Name))
                    {
                        //自动生成name
                        ab.Name = "alertButton" + (AlertCount + 1);
                        abo.Name = ab.Name;
                    }else
                    {
                        ab.Name = abo.Name;
                    }
                    //将事件选项设置到tag中
                }
                BoxHelper.btnOpts = btnOpts;
                //alertButton事件
                alert.ButtonClick += Alert_ButtonClick;
            }
            AlertCount++;
            alert.Show(owner, caption, descrption, autoCloseFormOnClick);
        }
        #endregion

        #region alertButtonClick事件
        private static void Alert_ButtonClick(object sender, AlertButtonClickEventArgs e)
        {
            if(BoxHelper.btnOpts != null && BoxHelper.btnOpts.Length > 0)
            {
                foreach(AlertButtonOption abo in BoxHelper.btnOpts)
                {
                    if(abo.Name == e.ButtonName)
                    {
                        abo.ExecuteClickEvent(sender, e);
                    }
                }
            }
        }
        #endregion

        #region Box相关
        public static DialogResult Confirm(IWin32Window owner, string Message)
        {
            try
            {
                SplashScreenManager.CloseForm();
            }
            catch (Exception) { }
            return XtraMessageBox.Show(owner, Message, "请确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
        }
        public static void Waring(IWin32Window owner, string Message)
        {
            try
            {
                SplashScreenManager.CloseForm();
            }
            catch (Exception) { }
            XtraMessageBox.Show(owner, Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        public static void Info(IWin32Window owner, string Message)
        {
            try
            {
                SplashScreenManager.CloseForm();
            }
            catch (Exception) { }
            XtraMessageBox.Show(owner, Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        public static void Error(IWin32Window owner, string Message)
        {
            try
            {
                SplashScreenManager.CloseForm();
            }
            catch (Exception) { }
            XtraMessageBox.Show(owner, Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        public static DialogResult Confirm(string Message)
        {
            try
            {
                SplashScreenManager.CloseForm();
            }
            catch (Exception) { }
            return XtraMessageBox.Show(Message, "请确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
        }
        public static void Waring(string Message)
        {
            try
            {
                SplashScreenManager.CloseForm();
            }
            catch (Exception) { }
            XtraMessageBox.Show(Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        public static void Info(string Message)
        {
            try
            {
                SplashScreenManager.CloseForm();
            }
            catch (Exception) { }
            XtraMessageBox.Show(Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        public static void Error(string Message)
        {
            try
            {
                SplashScreenManager.CloseForm();
            }
            catch (Exception) { }
            XtraMessageBox.Show(Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        #endregion
    }
    #region AlertButtonOption
    public class AlertButtonOption
    {
        /// <summary>
        /// ButtonName
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// Hint
        /// </summary>
        public string Hint { get; set; }
        /// <summary>
        /// 图片
        /// </summary>
        public Image Image { get; set; }
        /// <summary>
        /// Click事件
        /// </summary>
        public event AlertButtonClickEventHandler Click;
        /// <summary>
        /// 执行click事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void ExecuteClickEvent(object sender, AlertButtonClickEventArgs e)
        {
            Click?.Invoke(sender, e);
        }
    }
    #endregion
}
